import React, { memo, useCallback, useEffect, useImperativeHandle, useMemo, useRef, useState } from "react";
import { useModel } from "umi";
import { getDevGuidDataCwbbhd } from '../service';
import FormsTitle from "../components/FormsTitle";
import notify from "devextreme/ui/notify";
import { CpasIcon, DevGrid } from "~/public/cpas-ui";
import PageLoading from "@/pages/components/PageLoading";

const ReportForms = React.forwardRef((props: any, ref) => {
    const { devTableProps, slectYearData } = props;
    const { Data_ID, checkBoxVaue } = slectYearData;
    const { columnData, bqqm_wss, paramsName, bqqc_wss, tabName } = devTableProps;
    const { initialState } = useModel('@@initialState');
    const { dbname } = initialState?.curPart as API.CurPart || {};
    const DataRef = useRef<any>();
    const [devGuidData, setDevGuidData] = useState<any[]>([]);
    const [spinLoad, setSpinLoad] = useState<boolean>(false);


    function getGridInstance() {
        return DataRef.current.gridRef.current.instance;
    }

    /**questDevData 获取devGuid data 请求的数据
     * year_ID
     */
    const questDevData = useCallback(async (tabs_year_ID: string, tabs_checkBox: boolean, tabs_name?: string) => {
        setSpinLoad(true);
        try {
            const res = await getDevGuidDataCwbbhd({
                dbname,
                data_ID: tabs_year_ID,
                wcybxs: tabs_checkBox,
                cpasTableName: tabs_name || paramsName,
                bqqm_wss,
                bqqc_wss
            });
            if (res.success) {
                setDevGuidData(res.data);
                getGridInstance().pageIndex(0);
                notify(`获取数据成功！`, "success", 2000);
            } else {
                notify(`获取【account.ws.cwbbhd】的数据，失败！`, "error", 2000);
            }
        } catch (error) {
            notify(`获取【account.ws.cwbbhd】的数据，失败！`, "error", 2000);
        }
        setSpinLoad(false);

    }, []);


    // 自定义方法
    useImperativeHandle(ref, () => ({
        reloadDevTable: (tabs_year_ID: any, tabs_checkBox: boolean, tabs_name: string) => {
            questDevData(tabs_year_ID, tabs_checkBox, tabs_name);
        },
    }));

    useEffect(() => {
        if (Data_ID) {
            questDevData(Data_ID, checkBoxVaue);
        }
    }, [Data_ID, checkBoxVaue]);

    return (<div style={{ height: "calc(100% - 45px)" }}>
        {/* {spinLoad && <SpinLoading loadString="" />} */}
        <PageLoading
            isOpen={spinLoad}
            isMaskClose={false}
            content={""}
            width={200}
        />
        {/* 标题头 */}
        {/* <FormsTitle titleYearData={slectYearData} /> */}
        <DevGrid.Default
            width="100%"
            height="100%"
            dataSource={devGuidData}
            // rowAlternationEnabled={true}
            ref={DataRef}
            keyExpr="xh"
            className="devHeadersCenter PaperDevSortableGrid "
            defaultColumns={columnData}
            sheetName={`被审计单位提供报表数核对(${tabName})`}
            excelName={`被审计单位提供报表数核对(${tabName})`}
            showBorders={true}
            showRowLines={true}
            showColumnLines={true}
            paging={{
                enabled: false
            }}
        />
    </div >)
});
export default ReportForms;